home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 12: Textmags & Docs / nf_archive_12.iso / MAGS / SOURCES / ATARI_SRC.ZIP / atari source / AHDI / IDEINST / BOOKINST.C < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-09  |  6.4 KB  |  273 lines

  1. /* stbkinst.c */
  2.  
  3. /*
  4.  * Install hard disk driver onto C: of STBook
  5.  *
  6.  * Copyright 1991 Atari Corp.
  7.  * All Rights Reserved.
  8.  *
  9.  * Oct-21-1991    ml.    Created this from HINSTALL 5.00
  10.  */
  11.  
  12. #include "hinstall.h"
  13. #include "defs.h"
  14. #include "part.h"
  15. #include <gemdefs.h>
  16. #include <osbind.h>
  17.  
  18.  
  19. /* Storage wasted for idiotic bindings */
  20. int contrl[12];
  21. int intin[128];
  22. int ptsin[128];
  23. int intout[128];
  24. int ptsout[128];
  25.  
  26.  
  27. /* Error messages */
  28. static char *nomemory = "[3][Not enough system memory!|Cannot continue.][   OK   ]";
  29. static char *noavdrv = "[1][No available logical drive |for installation of driver.][   OK   ]";
  30. static char *cantinst = "[1][Cannot install driver!][   OK   ]";
  31. static char *rootread = "[3][Cannot read Root Sector|from the disk!|Try backing up the disk|and reformatting it.][   OK   ]";
  32. static char *rootwrit = "[3][Cannot write Root Sector|to the disk!|Try backing up the disk|and reformatting it.][   OK   ]";
  33. static char *bootread = "[3][Cannot read Boot Sector|from the logical drive!|Try backing up the disk|and reformatting it.][   OK   ]";
  34. static char *bootwrit = "[3][Cannot write Boot Sector|to the logical drive!|Try backing up the disk|and reformatting it.][   OK   ]";
  35. static char *wrdvrerr = "[3][Cannot write driver|file to destination!][   OK   ]";
  36. static char *crdvrerr = "[3][Cannot create driver|file at destination!][   OK   ]";
  37. static char *opdvrerr = "[3][Cannot open driver|source file!][   OK   ]";
  38. static char *drvinstd = "[1][Hard disk driver installed!][   OK   ]";
  39.  
  40. /* Globals */
  41. long ostack;            /* old system stack */
  42. int *pun;            /* pointer to # of physical units exist */
  43. int *vernum;            /* --> AHDI version #; 0 if not available */
  44.  
  45.  
  46. /*
  47.  * Top level;
  48.  *
  49.  */
  50. main()
  51. {
  52.     long *cookptr;    /* pointer to AHDI cookie */
  53.  
  54.     appl_init();
  55.  
  56.     Pexec(0, DVRSOURCE, "-s", 0);
  57.     ostack = Super(NULL);    /* Superuser mode for low memory access */
  58.     pun = (int *)(*(long *)PUNPTR);
  59.     Super(ostack);        /* back to User mode */
  60.  
  61.     if (!pun || !(*pun)) {    /* if no unit exists */
  62.     err(noavdrv);        /* return error */
  63.     goto punt;
  64.     }
  65.  
  66.     cookptr = (long *)((char *)pun + 2 + 16 + 16*4);
  67.     if (*cookptr != AHDI || cookptr != *(long *)(cookptr + 1))
  68.     vernum = 0L;
  69.     else
  70.     vernum = (int *)(cookptr + 2);
  71.  
  72.     install();
  73.     
  74. punt:
  75.     appl_exit();        /* exit application HDX */
  76.     Pterm(0);            /* terminate process */
  77. }
  78.  
  79.  
  80. /*
  81.  * Install hard disk driver.
  82.  *
  83.  */
  84. install()
  85. {
  86.     int i, pdev, ldev;
  87.     int maxsiz;
  88.     char *s, *d, sdev, dvr[15];
  89.     char *lbuf;
  90.     extern char rootstart, rootend;
  91.     extern char bootstart, bootend;
  92.  
  93.     /* find maximum sector size on system */
  94.     if (!vernum)        /* new version of AHDI? */
  95.     maxsiz = 512;        /* if not, sector size is always 512 bytes */
  96.     else
  97.     maxsiz = *(vernum + 1);    /* max sector size kept after version # */
  98.  
  99.     if (!(lbuf = Malloc((long)maxsiz))) {
  100.     err(nomemory);
  101.     return ERROR;
  102.     }
  103.  
  104.     ldev = 0;        /* C: */
  105.     pdev = 0x10;    /* IDE-AT unit 0 */
  106.     sdev = 'C';
  107.   
  108.     /* copy driver to specified unit */
  109.     if (copydvr(sdev) != OK)
  110.     return ERROR;
  111.  
  112.     /* read in the root sector */
  113.     if (getroot(pdev, lbuf) != 0) {
  114.     err(rootread);
  115.     goto argh;
  116.     }
  117.   
  118.     /* copy boot code into root sector */
  119.     for (d = lbuf, s = &rootstart, i = &rootend - &rootstart; i--;)
  120.     *d++ = *s++;
  121.  
  122.     /* if gemroot() is not successful, return with error */
  123.     if (gemroot(lbuf, 1) != 0) {
  124.     err(cantinst);
  125.     goto argh;
  126.     }
  127.  
  128.     /* write installed root sector back to disk */  
  129.     if (putroot(pdev, lbuf) != 0) {
  130.     err(rootwrit);
  131.     goto argh;
  132.     }
  133.  
  134.     /* read boot sector from partition */
  135.     if (getboot(ldev, lbuf) != 0) {
  136.     err(bootread);
  137.     goto argh;
  138.     }
  139.  
  140.     /* 
  141.      * copy boot code to boot sector, avoiding the BPB information 
  142.      * copy bytes 0..1 for BRA.S to code;
  143.      * leave bytes 2..$1d unaltered (information for BPB);
  144.      * copy bytes $1e..$1fe for code.
  145.      */
  146.     s = &bootstart;
  147.     d = lbuf;
  148.     *d++ = *s++;
  149.     *d++ = *s++;
  150.     d += 0x1c;
  151.     s += 0x1c;
  152.     for (i = &bootend-&bootstart-0x1e; i--;)
  153.     *d++ = *s++;
  154.  
  155.     /* make the image executable */
  156.     Protobt(lbuf, -1L, -1, 1);
  157.  
  158.     /* write the installed boot sector back to disk */
  159.     if (putboot(ldev, lbuf) != 0) {
  160.     err(bootwrit);
  161.     goto argh;
  162.     }
  163. /*
  164.     err(drvinstd);
  165. /**/
  166.     return;
  167.  
  168. argh:
  169.     dvr[0] = sdev;
  170.     strcpy(&dvr[1], DVRNAME);
  171.     Fdelete(dvr);
  172. }
  173.  
  174.  
  175. /*
  176.  * gemroot(buf, flg)
  177.  * char buf[]
  178.  * int flg    (0: clear boot bit;  non-0: set boot bit)
  179.  */
  180. gemroot(buf, flg)
  181. char buf[];
  182. int flg;
  183. {
  184.     GEMPART *gpart, *gpart1;
  185.     int pno;
  186.  
  187.     /* point to beginning of partition map */
  188.     gpart = gpart1 = &((RSECT *)(buf + 0x200 - sizeof(RSECT)))->hd_p[0];
  189.  
  190.     /* clear all boot bits */
  191.     for (pno = 0; pno < NPARTS; pno++, gpart1++) {
  192.     gpart1->p_flg &= ~P_BOOTBIT;
  193.     }
  194.  
  195.     if (!flg) {
  196.     Protobt(buf, -1L, -1, 1);
  197.     return OK;
  198.     }
  199.  
  200.     /* find the first valid partition and set its boot bit */
  201.     for (pno = 0; pno < NPARTS; pno++, gpart++) {
  202.     if (gpart->p_flg & P_EXISTS &&
  203.         gpart->p_siz) {         /* if partition exists */
  204.         if ((gpart->p_id[0] == 'G' &&    /* and it's a GEM partition */
  205.              gpart->p_id[1] == 'E' &&
  206.              gpart->p_id[2] == 'M') ||
  207.         (gpart->p_id[0] == 'B' &&
  208.          gpart->p_id[1] == 'G' &&
  209.          gpart->p_id[2] == 'M')) {
  210.             gpart->p_flg |= P_BOOTBIT;    /* set the boot bit */
  211.             Protobt(buf, -1L, -1, 1);    /* make image executable */
  212.             return OK;            /* return successful */
  213.         }
  214.     }
  215.     }
  216.     return ERROR;    /* if cannot find valid partition, return error */
  217. }
  218.  
  219.  
  220. /*
  221.  * Copy hard disk driver to root of sdev:
  222.  */
  223. char dvrbuf[BUFSIZ];
  224. copydvr(sdev)
  225. char sdev;
  226. {
  227.     int ih, oh, ret;
  228.     long cnt;
  229.     char dvrdest[15];    /* path name of driver */ 
  230.  
  231.     if ((ih = Fopen(DVRSOURCE, 0)) < 0)
  232.     return err(opdvrerr);
  233.   
  234.     dvrdest[0] = sdev;
  235.     strcpy(&dvrdest[1], DVRNAME);
  236.     if ((oh = Fcreate(dvrdest, 0)) < 0) {
  237.     ret = err(crdvrerr);
  238.     goto copyr;
  239.     }
  240.  
  241.     while ((cnt = Fread(ih, BUFSIZ, dvrbuf)) > 0) {
  242.     if (Fwrite(oh, cnt, dvrbuf) != cnt) {
  243.         ret = err(wrdvrerr);
  244.         goto copyr;
  245.     }
  246.     }
  247.     ret = OK;        /* got here with no error */
  248.  
  249. copyr:
  250.     Fclose(ih);
  251.     Fclose(oh);
  252.     if (ret) Fdelete(dvrdest);
  253.     return ret;
  254. }
  255.  
  256.  
  257. /*
  258.  * Throw up an alert box
  259.  * with the given text.
  260.  *
  261.  */
  262. err(s)
  263. char *s;
  264. {
  265.     graf_mouse(ARROW, 0L);
  266.     form_alert(1, s);
  267.     graf_mouse(HOURGLASS, 0L);
  268.     return ERROR;
  269. }
  270.  
  271.  
  272.  
  273.